package com.licenta.fbApp.server;

import java.util.ArrayList;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.licenta.fbApp.shared.Restaurant;


public class Database {
	
	/**
	 * @uml.property  name="dB"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="com.licenta.fbApp.shared.Restaurant"
	 */
	private ArrayList<Restaurant> DB = new ArrayList<Restaurant> ();
	
	@SuppressWarnings("deprecation")
	public Database()   {
	  	
	   DB = new ArrayList<Restaurant>();
	   
	   String line, aLink, aName, aAddress;
	   int aRating, aVotes, pos, nxt;
	   
	   File file = new File ("Info\\outbucuresti.txt");
	   FileInputStream fis = null;
	   BufferedInputStream bis = null;
	   DataInputStream br = null;
	   
	   try 
	   {		
		  fis = new FileInputStream(file);
	      bis = new BufferedInputStream(fis);
	      br = new DataInputStream(bis);
	      
	       while ((line = br.readLine()) != null) {
	    	   if ( line.indexOf("<h2><a href=\"/ro/detalii-loc/") >= 0 ){
	    		   break;
	    	   }
	       }
	       
	       /**
	        * Pozitionare pe primul link
	        * Ne intereseaza liniile care contin stringul "<h2><a href=\"/ro/detalii-loc/"
	        * Aceste linii contin url-ul ce trebuie accesat pentru detalii si numele restaurantului
	        * Pe urmatoarea linie se gaseste rating-ul si numarul de voturi 
	        * ex : 
	        * 	<h2><a href="/ro/detalii-loc/caru-cu-bere-b/bucuresti/">Caru' cu Bere</a></h2>Strada Stavropoleos 5
			*	<div>(<a href="/ro/detalii-loc/caru-cu-bere-b/bucuresti/">mai multe detalii</a>)</div><div class="rating-link lfloat">4 stele din 218 voturi</div>
	        */
	       
	       do {
	    	   pos = line.indexOf("href") +  6;
	    	   nxt = line.indexOf("\">", pos + 1);
	    	   aLink = line.substring(pos,  nxt - 1);
	    	   pos = line.indexOf("</a>", nxt);
	    	   aName = line.substring(nxt + 2 , pos );
	    	   aAddress = line.substring(line.indexOf("</h2>", nxt) + 5);
	    	   line = "";
	    	   line = br.readLine();
	    	   
	    	   try
	    	   {
		    	   pos = line.indexOf("rating-link");
		    	   pos = line.indexOf("\">", pos) + 2;
		    	   nxt = line.indexOf(" ", pos);		    	   
		    	   aRating = Integer.parseInt(line.substring(pos, nxt));		    	   
		    	   pos = line.indexOf("din", nxt) + 4;
		    	   nxt = line.indexOf(" ", pos);
		    	   aVotes = Integer.parseInt(line.substring(pos, nxt));    	   
	    	   } catch (NumberFormatException e) {
	    		   aRating = 0;
	    		   aVotes = 0;
	    	   }
	    	   
	    	   DB.add(new Restaurant(aName, aAddress, aRating, aVotes, aLink));
	    	   
	    	   while ((line = br.readLine()) != null) {
		    	   if ( line.indexOf("<h2><a href=\"/ro/detalii-loc/") >= 0 && line.indexOf("#comentarii") < 0 && line.indexOf("<img") < 0 ){
		    		   break;
		    	   }
		    	   line = "";
		       }	    	   
	       } while (line != null);
	       
	   }catch (IOException e) {
	         System.err.println("Error parsing file: " + e);
	       }
		   
	   /** S-au creat pe disc fisierele cu detalii pentru fiecare restaurant in parte
	    * Urmeaza parsarea si updatarea informatiilor existente
	    */
	   String tmp, myDiv;
	   for (int i = 0; i < DB.size(); i++) {	
		   tmp = DB.get(i).link.replace('/', ' ');
		   tmp = tmp.substring(tmp.substring(0, tmp.lastIndexOf(' ') - 2).lastIndexOf(' ') + 1 , tmp.lastIndexOf(' '));
    	   
		   try {
			  fis = new FileInputStream(new File("Info\\out" + tmp + ".txt"));
		      bis = new BufferedInputStream(fis);
		      br = new DataInputStream(bis);
    		   
    		   while ((line = br.readLine()) != null && (!line.contains("<div style=\"padding-right: 10px !important; overflow: hidden\">"))) {
    			   ;
			   }
    		   myDiv = line;
				while ((line = br.readLine()) != null && (!line.contains("<div class=\"mb10\" style=\"height:20px\">"))) {
					myDiv += line;
					line = "";
				   }
				DB.get(i).details = myDiv;
    	   } catch (Exception e){
    		   System.err.println(e.getMessage());
    	   }
	   }
	 }

	public ArrayList<Restaurant> getDB()
	{
		return this.DB;
	}
}

